﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;

namespace BDSim
{
    /// <summary>
    /// View3DScenario.xaml 的交互逻辑
    /// </summary>
    public partial class View3DScenario : UserControl
    {
        public View3DScenario()
        {
            InitializeComponent();
            CompositionTarget.Rendering += new System.EventHandler(CompositionTarget_Rendering);
        }

        #region 属性

        bool _IsShowCoord = true;

        public bool IsShowCoord
        {
            get { return _IsShowCoord; }
            set { _IsShowCoord = value; CoordDisplayControl(); }
        }

        bool _IsShowDayTime = true;

        public bool IsShowDayTime
        {
            get { return _IsShowDayTime; }
            set { _IsShowDayTime = value; ShowDayTime(); }
        }
        #endregion

        #region 字段
        /// <summary>
        /// 地球旋转控制
        /// </summary>
        private bool IsStopRotate = false;
        #endregion

        #region 3D场景初始化、结束控制
        /// <summary>
        /// 初始化3D场景
        /// </summary>
        public void Init()
        {
            foreach (var item in ((DataContext as MainWindowViewModel).Scenario.SegmentColletion[0] as SpaceSegment).AllSatellite)
            {
                viewport.Children.Add(item._3DModel._3DModel);
                viewport.Children.Add(item._3DModel._3DOrbitModel);
            }
        }

        /// <summary>
        /// 结束3D场景
        /// </summary>
        public void End()
        {
            foreach (var item in ((DataContext as MainWindowViewModel).Scenario.SegmentColletion[0] as SpaceSegment).AllSatellite)
            {
                viewport.Children.Remove(item._3DModel._3DModel);
                viewport.Children.Remove(item._3DModel._3DOrbitModel);
            }
        }

        #endregion

        #region 地球旋转
        void CompositionTarget_Rendering(object sender, EventArgs e)
        {

            if (BDSimObject.GlobalStatus == ESimStatus.Run)
            {
                YRotate1.Angle += 0.051;
            }
            if (YRotate1.Angle > 360)
                YRotate1.Angle = 0;
            if (IsStopRotate)
            {
                CompositionTarget.Rendering -= new System.EventHandler(CompositionTarget_Rendering);
                IsStopRotate = true;
            }
        }
        #endregion

        #region 显示/隐藏坐标轴
        void CoordDisplayControl()
        {
            switch (_IsShowCoord)
            {
                case true:
                    xAxis.Visibility = System.Windows.Visibility.Visible;
                    yAxis.Visibility = System.Windows.Visibility.Visible;
                    zAxis.Visibility = System.Windows.Visibility.Visible;
                    break;
                case false:
                    xAxis.Visibility = System.Windows.Visibility.Hidden;
                    yAxis.Visibility = System.Windows.Visibility.Hidden;
                    zAxis.Visibility = System.Windows.Visibility.Hidden;
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 显示白昼黑夜
        void ShowDayTime()
        {
            switch (_IsShowDayTime)
            {
                case true:
                    ambientLight.Color = Color.FromRgb(136,136,136);
                    break;
                case false:
                    ambientLight.Color = Color.FromRgb(255, 255, 255);
                    break;
                default:
                    break;
            }
        }
        #endregion

        private void ContextMenu_Click(object sender, RoutedEventArgs e)
        {
            string headerName = (e.OriginalSource as MenuItem).Header.ToString();
            switch (headerName)
            {
                case "显示/隐藏坐标":
                    IsShowCoord = !IsShowCoord;
                    break;
                case "显示黑夜白昼":
                    IsShowDayTime = !IsShowDayTime;
                    break;
                default:
                    break;
            }
            Point3D p = cam.Position;
        }
    }
}
